package net.kingborn.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import net.kingborn.common.exception.BizException;

/**
 * 日期工具
 * 
 * @author David Chen
 *
 */
public class DateKit {
	
	private static String dateFormatStr = "yyyy-MM-dd HH:mm:ss";
	public static ThreadLocal<DateFormat> dateFormatThreadLocal = new ThreadLocal<DateFormat>() {
		@Override
		protected DateFormat initialValue() {
			return new SimpleDateFormat(dateFormatStr);
		}
	};
	
	private static String shortDateFormatStr = "yyyy-MM-dd";
	public static ThreadLocal<DateFormat> shortDateFormatThreadLocal = new ThreadLocal<DateFormat>() {
		@Override
		protected DateFormat initialValue() {
			return new SimpleDateFormat(shortDateFormatStr);
		}
	};
	
	private static String birthFormatStr = "yyyy-MM";
	public static ThreadLocal<DateFormat> birthDateFormatThreadLocal = new ThreadLocal<DateFormat>() {
		@Override
		protected DateFormat initialValue() {
			return new SimpleDateFormat(birthFormatStr);
		}
	};

	public static SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm:ss");
	public static SimpleDateFormat hourMinuteFormatter = new SimpleDateFormat("HH:mm");
	public static SimpleDateFormat shortDateFormatter = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat yearMonthFormatter = new SimpleDateFormat("yyyy-MM");
	
	/**
	 * 将字符串转化为日期
	 * 
	 * @param dateStr
	 * @return
	 * @throws ParseException
	 */
	public static Date strToDate(String dateStr) {
		try {
			return dateFormatThreadLocal.get().parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
			
			return null;
		}
	}

	/**
	 * 将日期转化为字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String dateToStr(Date date) {
		return dateFormatThreadLocal.get().format(date);
	}

	/**
	 * 将短日期转化为字符串
	 *
	 * @param date
	 * @return
	 */
	public static String shortDateToStr(Date date) {
		return shortDateFormatThreadLocal.get().format(date);
	}

	/**
	 * 根据出生年月计算年龄
	 * @param birth
	 * 		形如：1988-08或1988-08-22
	 * @return
	 */
	public static Integer calculateAge(String birth) {
		if (StrKit.isBlank(birth)) {
			return 0;
		}
		
		try {
			Calendar calendar = Calendar.getInstance();
			int currentYear = calendar.get(Calendar.YEAR);
			
			if (birth.length() == 7) {
				calendar.setTime(birthDateFormatThreadLocal.get().parse(birth));
				
			} else {
				calendar.setTime(shortDateFormatThreadLocal.get().parse(birth));
			}
			int birthYear = calendar.get(Calendar.YEAR);
			
			return currentYear - birthYear + 1;
			
		} catch (ParseException e) {
			e.printStackTrace();
			
			return 0;
		}
	}

	/**
	 * 获取10位的时间
	 * @return
	 */
	public static Long currentTimeSecs() {
		return System.currentTimeMillis() / 1000;
	}
	
	/**
	 * 获取当前日期字符串(yyyy-MM-dd)
	 * 
	 * @return
	 */
	public static String currentDate() {
		return shortDateFormatThreadLocal.get().format(new Date());
	}
	
	/**
	 * 获取日期的星期
	 * 
	 * @param date
	 * 		形如：yyyy-MM-dd
	 * 		
	 * @return
	 */
	public static int dayOfWeek(String date) {
		Calendar calendar = null;
		try {
			calendar = Calendar.getInstance();
			calendar.setTime(shortDateFormatThreadLocal.get().parse(date));
			
		} catch (ParseException e) {
			e.printStackTrace();
			
			throw new BizException("日期格式不正确");
		}
		
		return calendar.get(Calendar.DAY_OF_WEEK);
	}
	
	public static void main(String[] args) {
		// System.out.println(dayOfWeek("2015-01-18"));
		System.out.println(calculateConstellation("1988-08-23"));
	}

	/**
	 * 计算星座
	 * 
	 * @param birthday
	 * @return
	 */
	public static String calculateConstellation(String birthday) {
		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(shortDateFormatThreadLocal.get().parse(birthday));
			return calculateConstellation(calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DATE));
			
		} catch (ParseException e) {
			e.printStackTrace();
			
			throw new BizException("日期格式不正确");
		}
	}
	
	/**
	 * 计算星座
	 * 
	 * @param month
	 * @param date
	 * @return
	 */
	public static String calculateConstellation(int month, int date) {
		String[] constellationArray = { "魔羯座", "水瓶座", "双鱼座", "牡羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座",
				"射手座", "魔羯座" };
		int[] constellationEdgeDate = { 20, 18, 20, 20, 20, 21, 22, 22, 22, 22, 21, 21 };
		
		if (date <= constellationEdgeDate[month - 1]) {
			month = month - 1;
		}
		
		if (month >= 0) {
			return constellationArray[month];
		}
		
		return constellationArray[11];
	}

	/**
	 * 获取第二天
	 * 
	 * @return
	 */
	public static Date tomorrow() {
		return null;
	}


}
